package org.bugjail.servlet.action;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bugjail.data.Person;
import org.bugjail.data.TestCaseExecution;
import org.bugjail.dataaccess.DAOFactory;
import org.bugjail.servlet.BasicServlet;

/**
 * Servlet implementation class for Servlet: TestCaseExecutionActionServlet
 * 
 */
public class TestCaseExecutionActionServlet extends BasicServlet implements
		javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	@Override
	protected void action(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String[] tcids = request.getParameterValues("checkid");
		List<TestCaseExecution> tceList = new LinkedList<TestCaseExecution>();
		for (String tcid : tcids) {
			int id = Integer.parseInt(tcid);
			TestCaseExecution tce = DAOFactory.getDAOFactory().getTestCaseExecutionDAO().retrieveById(id);
			tceList.add(tce);
		}
		
		if (request.getParameter("assignaction1") != null) {
			Person assignedUser = null;
			if (request.getParameter("assigneduser1")!=null) {
				int userId = Integer.parseInt(request.getParameter("assigneduser1"));
				assignedUser = DAOFactory.getDAOFactory().getPersonDAO().retrieveById(userId);
			}
			
			DAOFactory.getDAOFactory().getTestCaseExecutionDAO().assignTestCaseExecutionListToPerson(tceList, assignedUser);
		} else if (request.getParameter("assignaction2") != null) {
			Person assignedUser = null;
			if (request.getParameter("assigneduser2")!=null) {
				int userId = Integer.parseInt(request.getParameter("assigneduser2"));
				assignedUser = DAOFactory.getDAOFactory().getPersonDAO().retrieveById(userId);
			}
			
			DAOFactory.getDAOFactory().getTestCaseExecutionDAO().assignTestCaseExecutionListToPerson(tceList, assignedUser);
		} else if (request.getParameter("deleteaction1") != null || request.getParameter("deleteaction2") != null) {
			DAOFactory.getDAOFactory().getTestCaseExecutionDAO().deleteBasedOnList(tceList);
		} 
		response.sendRedirect("TestCaseExecutionServlet?testroundid="+request.getParameter("testroundid"));
	}

	@Override
	protected void beforeAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

	}

}